package models

import (
	"news/dao"
)

// User Model
type User struct {
	ID        int
	UserName  string
	PassWord  string
	RoleState bool
	Default   bool
	Region    string
	RoleID    int
}

// APIUser 返回给前端的用户结构
type APIUser struct {
	ID        int
	UserName  string
	RoleState bool
	Default   bool
	Region    string
	RoleID    int
}

/*
	增删查改在此处编写
*/

// CreateUser 创建用户
func CreateUser(user *User) (err error) {
	err = dao.DB.Create(&user).Error
	return
}

// GetAllUser 获取全部用户列表
func GetAllUser() (userList []*APIUser, err error) {
	err = dao.DB.Model(&User{}).Find(&userList).Error
	if err != nil {
		return nil, err
	}
	return
}

// GetAUser 获取一个用户的信息
func GetAUser(id string) (user *User, err error) {
	user = new(User)
	if err = dao.DB.Debug().Where("id=?", id).First(user).Error; err != nil {
		return nil, err
	}
	return
}

// GetAUserByLogin 登录专用获取用户
func GetAUserByLogin(username string) (user *User, err error) {
	user = new(User)
	err = dao.DB.Debug().Where("user_name=?", username).First(user).Error
	if err != nil {
		return nil, err
	}
	return user, err
}

// UpdateAUser 更新一个用户
func UpdateAUser(user *User) (err error) {
	err = dao.DB.Save(user).Error
	return
}

// DeleteAUser 删除一个用户
func DeleteAUser(id string) (err error) {
	err = dao.DB.Where("id=?", id).Delete(&User{}).Error
	return
}
